<div class="box box-solid no-margin box-show">
    <div class="box-body">
        <div class="box-body table-responsive no-padding">
            <table class="table table-hover">
                <thead>
                <tr>
                    <th style="border-bottom-color:#00a7d0;">批次号</th>
                    <th style="border-bottom-color:#00a7d0;">箱号</th>
                    <th style="border-bottom-color:#00a7d0;">FNSKU</th>
                    <th style="border-bottom-color:#00a7d0;">账号</th>
                    <th style="border-bottom-color:#00a7d0;">仓库</th>
                    <th style="border-bottom-color:#00a7d0;">站点</th>
                    <th style="border-bottom-color:#00a7d0;">SKU</th>
                    <th style="border-bottom-color:#00a7d0;">箱数</th>
                    <th style="border-bottom-color:#00a7d0;">单箱数量</th>
                    <th style="border-bottom-color:#00a7d0;">数量</th>
                </tr>
                </thead>
                <tbody>
                    @foreach($cis as $row)
                        <tr>
                            <td style="border-top-color:#00a7d0;">{{ $row->batch_no }}</td>
                            <td style="border-top-color:#00a7d0;">{{ $row->box_no }}</td>
                            <td style="border-top-color:#00a7d0;">{{ $row->fnsku }}</td>
                            <td style="border-top-color:#00a7d0;">{{ \App\Models\Basics\SysPlatAccount::getAccountById($row->account_id) ?? '' }}</td>
                            <td style="border-top-color:#00a7d0;">{{ \App\Models\OverSeasBa\Warehouse::getCodeById($row->warehouse_id) }}</td>
                            <td style="border-top-color:#00a7d0;">{{ \App\Models\Basics\SysArea::getSysAreaById($row->area_id) }}</td>
                            <td style="border-top-color:#00a7d0;">{{ $row->sku->psku_code ?? '' }}</td>
                            <td style="border-top-color:#00a7d0;">{{ $row->storage_total - $row->out_total }}</td>
                            <td style="border-top-color:#00a7d0;">{{ $row->num_per_box }}</td>
                            <td style="border-top-color:#00a7d0;">{{ ($row->storage_total - $row->out_total) * $row->num_per_box }}</td>
                        </tr>
                        @php
                            $items = \App\Models\OverSeasBa\InventoryBox::initRelatedData($row->account_id,$row->psku_id,$row->fnsku);
                        @endphp
                        @foreach($items as $item)
                            @if($item->account_id == $row->account_id && $item->fnsku == $row->fnsku)
                               @continue;
                            @else
                                <tr>
                                    <td><input type="checkbox" class="grid-row-checkbox" data-id="{{ $row->id }}" /></td>
                                    <td></td>
                                    <td>{{ $item->fnsku }}</td>
                                    <td>{{ \App\Models\Basics\SysPlatAccount::getAccountById($item->account_id) ?? '' }}</td>
                                    <td></td>
                                    <td></td>
                                    <td></td>
                                    <td>
                                        <input type="hidden" name="account_id" value="{{ $item->account_id }}">
                                        <input type="hidden" name="fnsku" value="{{ $item->fnsku }}">
                                        <input type="text" class="transfer_num_" style="width: 100px; text-align: right;" name="transfer_num"
                                               data-max="{{ $row->storage_total - $row->out_total }}" value="{{ $row->storage_total - $row->out_total }}">
                                    </td>
                                    <td></td>
                                    <td></td>
                                </tr>
                            @endif
                        @endforeach
                    @endforeach
                </tbody>
            </table>
        </div>
    </div>
</div>
<div style="margin-top: 10px;">
    <div class="box-body fields-group">
        <div class="form-group  ">
            <label class="col-sm-3  control-label"></label>
            <div class="col-sm-5">
                <label class="col-sm-5 control-label"></label>
                <button type="button" class="btn btn-primary" id="tran-submit">提交</button>
            </div>
        </div>
    </div>
</div>

<script language="JavaScript" type="text/javascript">
    function unique(arr) {
        return Array.from(new Set(arr))
    }
    $(function () {
        $('.grid-row-checkbox').iCheck({checkboxClass:'icheckbox_minimal-blue'}).on('ifChanged', function () {
            if (this.checked) {
                $(this).closest('tr').css('background-color', '#ffffd5');
            } else {
                $(this).closest('tr').css('background-color', '');
            }
        });

        var selectedRows = function () {
            var param = {};
            var selected = [];
            var detail = {};
            $('.grid-row-checkbox:checked').each(function(index){
                var id = $(this).data('id');
                selected.push(id);
                var indexArr = {};
                indexArr['id'] = id;
                var tr = $(this).closest('tr');
                indexArr['account_id'] = tr.find('input[name="account_id"]').val();
                indexArr['fnsku'] = tr.find('input[name="fnsku"]').val();
                indexArr['transfer_num'] = tr.find('input[name="transfer_num"]').val();
                detail[index] = indexArr;
            });
            param.selected = selected;
            param.detail = detail;
            return param;
        };

        $('#tran-submit').on('click', function() {
            $(this).prop('disabled',true);
            var param = selectedRows();
            var ids = param.selected;
            if(ids.length == 0) {
                $(this).prop('disabled',false);
                swal('请选择单据', '', 'error');
                return false;
            }
            var detail = param.detail;
            $.ajax({
                method: 'post',
                url: '/xqc/overseas-ba/box-in-ajax',
                data: {
                    _token:"{{ csrf_token() }}",
                    param: { detail : detail },
                    action: 'transfer'
                },
                success: function (data) {
                    if (typeof data === 'object') {
                        if (data.status) {
                            $.pjax.reload('#tran-form');
                            toastr.success('提交成功 !');
                        } else {
                            swal(data.message, '', 'error');
                        }
                    }
                }
            });
        });
        $(".transfer_num_").inputmask("integer",
            {
                onKeyValidation: function () {
                    if($(this).val() > $(this).data('max') || $(this).val() < 0){
                        $(this).val($(this).data('max'));
                    }
                }
            }
        );
    });
</script>
